/*
 * TODO: License
 */

package com.rppowell.pixie.swing.map;

import com.rppowell.pixie.models.Map;
import com.rppowell.pixie.models.Sprite;
import com.rppowell.pixie.models.SpriteSetProperties;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JPanel;

/**
 *
 * @author rppowell
 */
public class MapCanvasPanel extends JPanel implements MouseListener{
    
    private Map map;

    public MapCanvasPanel(Map map) {
        this.map = map;

        Dimension d = map.getImageSize();
        
        addMouseListener(this);
        setPreferredSize(d);
    }
        
    public void paint(Graphics g) {
        Graphics2D g2d = (Graphics2D)g;
        Sprite s;
        Rectangle r;
        
        g2d.setColor(getBackground());
        g2d.fillRect(0, 0, getWidth(), getHeight());
        
        for(int j = 0; j<map.getMapSize().height; j++) {
            for(int i =0; i<map.getMapSize().width; i++) {
                s = map.getSprite(i, j);
                if (s != null) {
                    r = new Rectangle(i*s.getWidth(),j*s.getHeight(), s.getWidth(), s.getHeight());
                    g2d.drawImage(s,r.x, r.y, r.width, r.height, this);
                }
            }
        }
    }
    
    public Point canvasToCoordinates(Point p) {
        SpriteSetProperties properties = map.getSpriteSet().getSpriteSetProperties();
        return new Point(p.x / properties.getSpriteWidth(), p.y / properties.getSpriteWidth());
    }

    public void mouseClicked(MouseEvent me) {
        System.out.println("mouseClicked("+me+")");
        System.out.println("mouseClicked("+canvasToCoordinates(me.getPoint())+")");
    }

    public void mousePressed(MouseEvent me) { }

    public void mouseReleased(MouseEvent me) { }

    public void mouseEntered(MouseEvent me) { }

    public void mouseExited(MouseEvent me) { }
}
